/**
 *
 *
 * 设置系统托盘
 *
 *
 */

const { Tray, Menu, nativeImage, app } = require('electron')
import appIcon from '../../../resources/icon.png?asset'

export default class TrayService {
  constructor(mainWindow) {
    this.mainWindow = mainWindow
    let template = [
      {
        label: '窗口',
        submenu: [
          {
            label: '最小化',
            accelerator: `ctrl+n`,
            click: () => {}
          },
          {
            label: '最大化',
            accelerator: `ctrl+b`,
            click: () => {}
          }
        ]
      },
      {
        label: '应用',
        submenu: [{ label: '关于' }, { label: '联系我们' }]
      },
      {
        label: '退出',
        click: () => {
          app.quit()
        }
      }
    ]
    const contextMenu = Menu.buildFromTemplate(template)
    let tray = new Tray(nativeImage.createFromPath(appIcon))
    tray.setContextMenu(contextMenu)
    tray.setToolTip('TestStudySystem')
    tray.setTitle('TestStudySystem')
    tray.setPressedImage(nativeImage.createFromPath(appIcon))
    if (process.platform === 'win32') {
      tray.on('click', () => {
        this.mainWindow.show()
      })
    }

    // 对于macOS而言，Tray还有一个很棒的特性——可以拖拽文件到Tray的icon上，会触发如下事件：
    // drop 当任何东西拖拽到icon上时
    // drop-files 当文件被拖拽到icon上时
    // drop-text 当文本被拖拽到icon上时
    // drop-enter 当刚拖拽到icon上时
    // drop-leave 当拖拽事件离开icon时
    // drop-end 当拖拽事件结束时
    if (process.platform !== 'win32') {
      tray.on('drop-files', () => {})
      tray.on('drop-text', () => {})
      tray.on('drop-enter', () => {})
      tray.on('drop-leave', () => {})
      tray.on('drop-end', () => {})
    }
  }
}
